resource "aws_instance" "example" {
  ami           = var.ami
  count          = var.instance_count
  instance_type = var.instance_type
  subnet_id     = element(var.subnet_ids, count.index % length(var.subnet_ids))
  vpc_security_group_ids = var.security_groups
  key_name      = aws_key_pair.deployer.key_name

  root_block_device {
    volume_type = "gp2"
    volume_size = 20
  }
}

resource "aws_lb_target_group_attachment" "this" { 
  count            = var.instance_count
  target_group_arn = var.target_group_arn
  target_id        = aws_instance.example[count.index].id
  port             = 80
}

resource "aws_key_pair" "deployer" {
  key_name   = "deployer-key"
  public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDIYoOvj2rxFy0970EHKoRxYCIp03TctMbVlvntT9FT2kVgnmBzl459n9cRP1vLxoJ+kND+kd/ou9lflXEl+u++vcbifZu5zdc7pcLvwQ9QKNxKO4LQPFD9fEZiC8kIFP77ifRBwIKfd0Mu5RZw4UR9ddzLFAcb51hjGZpQl0PCVa8REMhvKLb9QR23VPUgtxmdFJZnBlkOVTBFSZbNyDLoiTM3uSyPrrWv1Qar5PO+Tr0d1YHIxerOlnCYRTYlvX16bwDpAwHCA3k5rT4J3kMtNc2ho9OfCcdDZDnqb7Dr3eIWkngZNaCHNs/+KKyUBfSYKLmaCYBivxDnx/JGDS7H root@flexusx-7612-tt"
}

resource "aws_eip" "example" {
  instance = aws_instance.example[0].id
  vpc      = true
}

output "instance_ids" {
  value = aws_instance.example[*].id
}

